/// @tags: SAM SegmentTree
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using std::cin, std::cout;

namespace BlueQuantum {

typedef long long LL;
typedef std::pair<int, int> PII;

constexpr int MAXN = 1e5 + 3, SAMN = MAXN * 2, SEGN = SAMN * 4, CHARSET = 26;

namespace SuffixAutoMaton {

int samcnt = 1;
int link[SAMN], ch[SAMN][CHARSET], len[SAMN];

inline int extend(int c) {
  static int lst = 1;
  int cur = ++samcnt, p = lst;
  lst = cur;
  len[cur] = len[p] + 1;
  while (p != 0 && ch[p][c] == 0) ch[p][c] = cur, p = link[p];
  if (p == 0) {
    link[cur] = 1;
    return cur;
  }
  int q = ch[p][c];
  if (len[q] == len[p] + 1) {
    link[cur] = q;
    return cur;
  }
  int clone = ++samcnt;
  memcpy(ch[clone], ch[q], sizeof(ch[clone]));
  link[clone] = link[q];
  len[clone] = len[p] + 1;
  link[q] = link[cur] = clone;
  while (p != 0 && ch[p][c] == q) ch[p][c] = clone, p = link[p];
  return cur;
}

}  // namespace SuffixAutoMaton
using SuffixAutoMaton::samcnt, SuffixAutoMaton::extend;

namespace SegmentTree {

struct Series {
  LL k, b;

  inline Series& operator+=(const Series& rhs) {
    k += rhs.k, b += rhs.b;
    return *this;
  }

  inline LL integral(int l, int r) const { return (k * (l + r) + 2 * b) * (r - l + 1) / 2; }
};

struct Node {
  LL sum;
  Series tag;
} tr[SEGN];

inline void pushUp(int x) { tr[x].sum = tr[x << 1].sum + tr[x << 1 | 1].sum; }

inline void applyTag(int x, int l, int r, const Series& w) {
  tr[x].tag += w;
  tr[x].sum += w.integral(l, r);
}

inline void pushDown(int x, int l, int r) {
  if (tr[x].tag.k || tr[x].tag.b) {
    int mid = (l + r) >> 1;
    applyTag(x << 1, l, mid, tr[x].tag);
    applyTag(x << 1 | 1, mid + 1, r, tr[x].tag);
    tr[x].tag = Series{0, 0};
  }
}

void modify(int x, int l, int r, int L, int R, const Series& w) {
  if (L <= l && r <= R) return applyTag(x, l, r, w);
  pushDown(x, l, r);
  int mid = (l + r) >> 1;
  if (L <= mid) modify(x << 1, l, mid, L, R, w);
  if (mid < R) modify(x << 1 | 1, mid + 1, r, L, R, w);
  pushUp(x);
}

LL query(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) return tr[x].sum;
  pushDown(x, l, r);
  int mid = (l + r) >> 1;
  LL res = 0;
  if (L <= mid) res += query(x << 1, l, mid, L, R);
  if (mid < R) res += query(x << 1 | 1, mid + 1, r, L, R);
  return res;
}

}  // namespace SegmentTree
using SegmentTree::modify, SegmentTree::query;

int n;

namespace LinkCutTree {

using SegmentTree::Series;
using SuffixAutoMaton::len;

struct Node {
  int ch[2], fa, tag, col;
} tr[SAMN];

inline bool iden(int x) { return tr[tr[x].fa].ch[1] == x; }

inline bool isnrt(int x) { return tr[tr[x].fa].ch[0] == x || tr[tr[x].fa].ch[1] == x; }

inline void rotate(int x) {
  bool xtofa = iden(x);
  int fa = tr[x].fa, ffa = tr[fa].fa, son = tr[x].ch[!xtofa];
  if (isnrt(fa)) tr[ffa].ch[iden(fa)] = x;
  tr[x].fa = ffa;
  tr[fa].fa = x;
  tr[x].ch[!xtofa] = fa;
  tr[fa].ch[xtofa] = son;
  if (son) tr[son].fa = fa;
}

inline void pushDown(int x) {
  if (tr[x].tag) {
    int ls = tr[x].ch[0], rs = tr[x].ch[1];
    if (ls) tr[ls].tag = tr[ls].col = tr[x].tag;
    if (rs) tr[rs].tag = tr[rs].col = tr[x].tag;
    tr[x].tag = 0;
  }
}

inline void pushAll(int x) {
  if (isnrt(x)) pushAll(tr[x].fa);
  pushDown(x);
}

inline void splay(int x) {
  pushAll(x);
  for (int fa; isnrt(x); rotate(x)) {
    if (isnrt(fa = tr[x].fa)) rotate(iden(x) == iden(fa) ? fa : x);
  }
}

inline void access(int x, int id) {
  int y = 0;
  for (; x; y = x, x = tr[x].fa) {
    splay(x);
    tr[x].ch[1] = y;
    if (tr[x].col)
      modify(1, 1, n, tr[x].col - len[x] + 1, tr[x].col - len[tr[x].fa], Series{1, -tr[x].col - 1});
  }
  tr[y].col = tr[y].tag = id;
  modify(1, 1, n, 1, id, Series{-1, id + 1});
}

inline void build() {
  for (int i = 1; i <= samcnt; ++i) tr[i].fa = SuffixAutoMaton::link[i];
}

}  // namespace LinkCutTree
using LinkCutTree::access;

int pos[MAXN];
char str[MAXN];
LL ans[MAXN];
std::vector<PII> q[MAXN];

inline int main() {
  cin >> (str + 1);
  n = strlen(str + 1);
  for (int i = 1; i <= n; ++i) pos[i] = extend(str[i] - 'a');
  int m;
  cin >> m;
  for (int l, r, i = 1; i <= m; ++i) {
    cin >> l >> r;
    q[r].push_back(std::make_pair(l, i));
  }
  LinkCutTree::build();
  for (int i = 1; i <= n; ++i) {
    access(pos[i], i);
    for (std::vector<PII>::iterator j = q[i].begin(), jlim = q[i].end(); j != jlim; ++j) {
      ans[j->second] = query(1, 1, n, j->first, i);
    }
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("string.in", "r", stdin);
  freopen("string.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
